﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity.PPSM2007;
using JM.JCY.Proxy;
using Microsoft.SharePoint;
using JM.JCY.BusinessEntity;
using System.Linq;
using System.Data;
using JM.JCY.Common;

namespace JM.JCY.SharePointComponent.PersonalInformationDocument.SelfResumeRecord
{

    public partial class SelfResumeRecordUserControl : UserControl
    {
        PPSM2007CodeDataSet.T_PPSM2007CODEDataTable CodeTable;
        int ID = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CodeTable = GetPPSM2007CODEData();
                //SPWeb web = SPContext.Current.Web;
                var currentUserName = SPContext.Current.Web.CurrentUser.Name;
                //var currentUserName = "郭鸿麒";
                    //web.CurrentUser.Name;
                var userInfo = GetUserInfoByName(currentUserName);
                if (userInfo.Rows.Count > 0)
                    BindUserInfo(userInfo);

                DataRow[] rows = GetCareerByName(currentUserName).Select();
                dlJob.DataSource = rows;
                dlJob.DataBind();
                if (rows.Count() > 0)
                {
                    
                    NoRecord.Visible = false;
                }
                   
                else
                    NoRecord.Visible = true;
            }
        }

        protected void dlJob_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserInfoDataSet.UserCareerRow row = (PPSMUserInfoDataSet.UserCareerRow)e.Item.DataItem;
            Label lbID = (Label)e.Item.FindControl("lbID");
            Label lbStartDateTime = (Label)e.Item.FindControl("lbStartDateTime");
            Label lbEndDateTime = (Label)e.Item.FindControl("lbEndDateTime");
            Label lbJobAndPost = (Label)e.Item.FindControl("lbJobAndPost");
            Label lbAdministrativeGrade2 = (Label)e.Item.FindControl("lbAdministrativeGrade2");
            lbID.Text = Convert.ToString(e.Item.ItemIndex + 1);
            lbStartDateTime.Text = string.IsNullOrEmpty(row.BeginDate) ? string.Empty : row.BeginDate.Substring(0, 4).ToString() + "年"+row.BeginDate.Substring(4, 2).ToString() +"月"+row.BeginDate.Substring(6, 2).ToString() +"日";
            lbEndDateTime.Text = string.IsNullOrEmpty(row.EndDate) ? string.Empty : row.EndDate.Substring(0, 4).ToString() + "年" + row.EndDate.Substring(4, 2).ToString() + "月" + row.EndDate.Substring(6, 2).ToString() + "日";
            lbJobAndPost.Text = string.IsNullOrEmpty(row.UserCareer) ? string.Empty : row.UserCareer;
            lbAdministrativeGrade2.Text = string.IsNullOrEmpty(row.AdminRank) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "行政职级", row.AdminRank);
            ID++;
        }

        public PPSMUserInfoDataSet.UserInfoDataTable GetUserInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetUserInfoByName(userName);
            }
        }

        public PPSMUserInfoDataSet.UserCareerDataTable GetCareerByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetCareerByName(userName);
            }
        }
        public void BindUserInfo(PPSMUserInfoDataSet.UserInfoDataTable userInfoDT)
        {
            var codeTable = CodeTable;
            var row = userInfoDT[0];
            this.lbName.Text = row.UserName;
            this.lbGender.Text = row.UserSex == "1" ? "男" : "女";
            this.lbBirth.Text = string.IsNullOrEmpty(row.UserBirthday) ? string.Empty : row.UserBirthday.Substring(0, 4);
        
            this.lbNation.Text = string.IsNullOrEmpty(row.UserNationality) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "民族", row.UserNationality);
            this.lbBirthplace.Text = string.IsNullOrEmpty(row.UserNativePlace) ? string.Empty : row.UserNativePlace;
            this.lbMarriage.Text = string.IsNullOrEmpty(row.UserMarriageState) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "婚姻状况", row.UserMarriageState);
            this.lbHealth.Text = string.IsNullOrEmpty(row.UserHealth) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "健康状况", row.UserHealth);
            this.lbPolitics.Text = string.IsNullOrEmpty(row.UserClan) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "政治面貌", row.UserClan);
            this.lbEducation.Text = string.IsNullOrEmpty(row.UserNowGraduate) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "当前学历", row.UserNowGraduate);
            var userUniversityName = string.IsNullOrEmpty(row.UserUniversityName) ? string.Empty : row.UserUniversityName;
            var userSpecialtyName = string.IsNullOrEmpty(row.UserSpecialtyName) ? string.Empty : row.UserSpecialtyName;
            this.lbSchool.Text = userUniversityName + " (" + userSpecialtyName+")";
            this.lbPartyPost.Text = string.IsNullOrEmpty(row.UserPartyBusiness) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "党组任职职务", row.UserPartyBusiness);
            this.lbAdministrativePost.Text = string.IsNullOrEmpty(row.UserAdminBusiness) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "行政职务", row.UserAdminBusiness);
            this.lbLegislationPost.Text = string.IsNullOrEmpty(row.UserLegalBusiness) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "法律职务", row.UserLegalBusiness);
            this.lbAdministrativeGrade.Text = string.IsNullOrEmpty(row.UserAdminRank) ? string.Empty : GetCodeDescByCategoryAndValue(codeTable, "行政职级", row.UserAdminRank);
            this.lbJobName.Text = string.IsNullOrEmpty(row.UserWorkerRankName) ? string.Empty : row.UserWorkerRankName;

            
            if (row.IsImgNull())
                if(row.UserSex == "1")
                    UserImg.ImageUrl = JCYConstant.StaffProfile.DefaultPicturePathMan;
                else
                    UserImg.ImageUrl = JCYConstant.StaffProfile.DefaultPicturePathWoman;
            else
                UserImg.ImageUrl = "~/_Layouts/SharePointComponent/UserImage.aspx?UserName=" + row.UserName;
        } 

        public string GetCodeDescByCategoryAndValue(PPSM2007CodeDataSet.T_PPSM2007CODEDataTable dt,string category, string value)
        {
            try
            {
                var codeDesc = from codeTable in dt
                               where codeTable.Category == category && codeTable.CodeValue == value
                               select codeTable.CodeDesc;
                return codeDesc.First();
            }
            catch (Exception)
            {

                return "";
            }
         

        
        }

        public PPSM2007CodeDataSet.T_PPSM2007CODEDataTable GetPPSM2007CODEData()
        {
            var dt = new PPSM2007CodeDataSet.T_PPSM2007CODEDataTable();
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetPPSM2007CodeTable();
            }
        }

       
    }
}
